package controller.parents;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pojos.common.ReplyMessage;
import pojos.parents.ParentsInfo;
import service.CommonUtilsService;
import service.ParentsInfoService;
import utils.InputVerifyUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 家长信息控制器
 */
@Controller
@RequestMapping(value = "/parents")
public class ParentsInfoController {

    private final Logger logger = LoggerFactory.getLogger(ParentsInfoController.class);

    @Resource(name = "parentsInfoService")
    private ParentsInfoService parentsInfoService;

    @Resource(name = "commonUtilsService")
    private CommonUtilsService utilsService;

    /**
     * 家长修改基础信息
     */
    @PostMapping("/frame/alterBaseInfo-submit")
    @ResponseBody
    public ReplyMessage alterBaseInfoSubmit(@RequestParam(value = "realName",required = false) String realName,
                                            @RequestParam(value = "sex",required = false) Integer sex,
                                            HttpServletRequest request){
        HttpSession session = request.getSession();
        ParentsInfo parentsInfoSession = (ParentsInfo) session.getAttribute("UserInfo");
        ParentsInfo parentsInfo = new ParentsInfo();

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            BeanUtils.copyProperties(parentsInfo,parentsInfoSession);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("家长修改基础信息时复制对象异常，教师ID："+parentsInfoSession.getId());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }

        if(realName != null && realName.length() > 0){
            if(InputVerifyUtils.isRealNameNotRight(realName)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("真实姓名不正确！");
                return replyMessage;
            }
            parentsInfo.setRealName(realName);
        }

        if(sex != null){
            parentsInfo.setSex(sex);
        }

        replyMessage = parentsInfoService.parentsAlterBaseInfo(parentsInfo);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",parentsInfo);
        }

        return replyMessage;
    }

    /**
     * 家长修改登录名称
     */
    @PostMapping("/frame/alterLoginName-submit")
    @ResponseBody
    public ReplyMessage alterLoginNameSubmit(@RequestParam(value = "loginName",required = false) String name,
                                             HttpServletRequest request){
        HttpSession session = request.getSession();
        ParentsInfo parentsInfoSession = (ParentsInfo) session.getAttribute("UserInfo");
        ParentsInfo parentsInfo = new ParentsInfo();

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            BeanUtils.copyProperties(parentsInfo,parentsInfoSession);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("家长修改登录名称复制对象时异常，家长ID："+parentsInfoSession.getId());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }

        if(InputVerifyUtils.isLoginNameNotRight(name)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名称不正确！");
            return replyMessage;
        }

        parentsInfo.setName(name);
        replyMessage = parentsInfoService.alterLoginName(parentsInfo);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",parentsInfo);
        }

        return replyMessage;
    }

    /**
     * 家长修改登录密码
     */
    @PostMapping("/frame/alterPassword-submit")
    @ResponseBody
    public ReplyMessage alterLoginPassword(@RequestParam(value = "oldPassword",required = false) String oldPassword,
                                           @RequestParam(value = "newPassword",required = false) String newPassword,
                                           HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();

        if(InputVerifyUtils.isPasswordNotRight(oldPassword)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("原密码不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isPasswordNotRight(newPassword)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("新密码不符合要求！");
            return replyMessage;
        }

        HttpSession session = request.getSession();
        ParentsInfo parentsInfo = (ParentsInfo) session.getAttribute("UserInfo");
        return parentsInfoService.alterPassword(oldPassword,newPassword,parentsInfo);
    }

    /**
     * 家长修改手机号
     */
    @PostMapping("/frame/alterPhone-submit")
    @ResponseBody
    public ReplyMessage parentsAlterPhone(@RequestParam(value = "imgCode",required = false) String imgCode,
                                          @RequestParam(value = "smsCode",required = false) String smsCode,
                                          @RequestParam(value = "phone",required = false) String phone,
                                          HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();

        if(InputVerifyUtils.isImgAuthCodeNotRight(session,imgCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("图片验证码不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isSmsCodeNotRight(session,smsCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("短信验证码不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isPhoneNotRight(phone)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("手机号不正确！");
            return replyMessage;
        }

        ParentsInfo parentsInfoSession = (ParentsInfo) session.getAttribute("UserInfo");
        ParentsInfo parentsInfo = new ParentsInfo();
        try {
            BeanUtils.copyProperties(parentsInfo,parentsInfoSession);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("家长修改手机号复制对象时异常，家长ID："+parentsInfoSession.getId());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }

        // 清空图片验证码和短信验证码Session值
        session.removeAttribute("ImgAuthCode");
        session.removeAttribute("SmsAuthCode");

        parentsInfo.setPhone(phone);
        replyMessage = parentsInfoService.alterPhone(parentsInfo);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",parentsInfo);
        }

        return replyMessage;
    }

    /**
     * 家长修改联系信息
     */
    @PostMapping("/frame/alterConn-submit")
    @ResponseBody
    public ReplyMessage parentsAlterConnInfo(@RequestParam(value = "QQNum",required = false) String QQNum,
                                             @RequestParam(value = "weChatNum",required = false) String weChatNum,
                                             HttpServletRequest request){
        HttpSession session = request.getSession();
        ParentsInfo parentsInfoSession = (ParentsInfo) session.getAttribute("UserInfo");
        ParentsInfo parentsInfo = new ParentsInfo();

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            BeanUtils.copyProperties(parentsInfo,parentsInfoSession);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("家长修改联系复制对象时异常，家长ID："+parentsInfoSession.getId());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }

        if(QQNum != null && QQNum.length() > 0){
            if(InputVerifyUtils.isQQNotRight(QQNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("QQ号码不正确！");
                return replyMessage;
            }
            parentsInfo.setQQ(QQNum);
        }

        if(weChatNum != null && weChatNum.length() > 0){
            if(InputVerifyUtils.isWeChatNotRight(weChatNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("微信号不正确！");
                return replyMessage;
            }
            parentsInfo.setWeChat(weChatNum);
        }

        replyMessage = parentsInfoService.alterConnInfo(parentsInfo);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",parentsInfo);
        }

        return replyMessage;
    }

    /**
     * 家长查找头像信息
     */
    @PostMapping("/frame/query-parents-avatar")
    @ResponseBody
    public ReplyMessage queryParentsAvatar(HttpServletRequest request){
        HttpSession session = request.getSession();
        ParentsInfo parentsInfo = (ParentsInfo) session.getAttribute("UserInfo");

        ReplyMessage replyMessage = new ReplyMessage();
        replyMessage.setStatus(200);
        replyMessage.setInfo(parentsInfo.getAvatar());
        return replyMessage;
    }

    /**
     * 家长修改头像信息
     */
    @PostMapping("/frame/alter-parents-avatar-submit")
    @ResponseBody
    public ReplyMessage parentsAlterAvatar(@RequestParam(value = "avatar",required = false) MultipartFile avatar,
                                           HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();

        if(InputVerifyUtils.isAvatarNotRight(avatar)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请上传正确的图片文件！");
            return replyMessage;
        }

        HttpSession session = request.getSession();
        ParentsInfo parentsInfo = (ParentsInfo) session.getAttribute("UserInfo");

        replyMessage = utilsService.alterAvatar(avatar,parentsInfo.getAvatar(),parentsInfo.getId(),4);
        if(replyMessage.getStatus() == 200){
            parentsInfo.setAvatar(replyMessage.getInfo());
        }

        return replyMessage;
    }

    /**
     * 家长查询课程信息
     */
    @PostMapping("/frame/current-class-info")
    @ResponseBody
    public ReplyMessage parentsCurrentClassInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        ParentsInfo parentsInfo = (ParentsInfo) session.getAttribute("UserInfo");
        return parentsInfoService.queryParentsCurrentClassInfo(parentsInfo.getId());
    }
}
